package cn.onlineclass.service;

import cn.onlineclass.dao.MajorMapper;
import cn.onlineclass.entity.OMajor;
import cn.onlineclass.util.MybatisUtil;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

public class MajorService {
    public static final MajorService MAJOR=new MajorService();

    //查询分页
    public AjaxPage search(int pageNo, int pageSize){
        SqlSession s= MybatisUtil.get();
        try {
            MajorMapper mapper = s.getMapper(MajorMapper.class);
            List<OMajor> search = mapper.search(pageNo, pageSize);
            int total = mapper.searchTotal();
            return new AjaxPage(search,total,10,pageNo);
        }finally {
            MybatisUtil.close(s);
        }
    }

    //增加
    public AjaxResult add(OMajor c){
        SqlSession s= MybatisUtil.get();
        try {
            MajorMapper mapper = s.getMapper(MajorMapper.class);
            int add = mapper.add(c);
            return  AjaxResult.success(add);
        }finally {
            MybatisUtil.close(s);
        }
    }

    //修改
    public AjaxResult update(OMajor c){
        SqlSession s = MybatisUtil.get();
        try {
            MajorMapper mapper = s.getMapper(MajorMapper.class);
            int update = mapper.update(c);
            return AjaxResult.success(update);
        }finally {
            MybatisUtil.close(s);
        }
    }

    //删除
    public AjaxResult del(long id){
        SqlSession s = MybatisUtil.get();
        try {
            MajorMapper mapper = s.getMapper(MajorMapper.class);
            int del = mapper.del(id);
            return AjaxResult.success(del);
        }finally {
            MybatisUtil.close(s);
        }
    }



    //根据id查询单个信息
    public AjaxResult get(long id){
        SqlSession s =MybatisUtil.get(true);
        try {
            MajorMapper mapper = s.getMapper(MajorMapper.class);
            OMajor major = mapper.get(id);
            return AjaxResult.success(major);
        }finally {
            MybatisUtil.close(s);
        }
    }


}
